<template>
  <header class="header">
    <h1>todos</h1>
    <input id="toggle-all" class="toggle-all" type="checkbox" @click="changeisall" v-model="isAll" >
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      @click.enter="addtask"
      v-model="ipt"
    />
  </header>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      ipt: ''
    }
  },
  computed: {
    isAll: {
      get () {
        return this.arr.every(item => item.completed === true)
      },
      set (val) {
        this.arr.forEach(item => (item.completed = val))
      }
    }
  },
  methods: {
    addtask () {
      axios({
        url: '/add',
        method: 'post',
        data: {
          title: this.ipt
        }
      }).then(res => {
        console.log(res)
      })
      this.$emit('rendertask')
      this.ipt = ''
    },
    changeisall () {
      const bol = this.isAll === true ? 1 : 0
      axios({
        url: 'all/' + bol
      }).then(res => {
        console.log(res)
      })
    }
  },
  props: ['arr']
}
</script>
